package com.ling.pl.security.view.group;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import com.ling.core.common.utils.D7PageUtils;
import com.ling.pl.security.feign.DefaultGroupService;
import com.ling.pl.security.feign.DefaultOrgService;
import com.ling.pl.security.feign.DefaultPositionService;
import com.ling.pl.security.feign.DefaultUserService;
import com.ling.pl.security.model.DefaultGroupVO;
import com.ling.pl.security.model.GroupMemberVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author bo.wang
 * @since 2013-2-18
 */
@Component("ling2.groupMaintain")
public class GroupMaintain {
    @Resource
    DefaultGroupService groupService;
    @Resource
    private DefaultUserService userService;
    @Resource
    private DefaultOrgService orgService;
    @Resource
    private DefaultPositionService positionService;

    @DataProvider
    public void loadGroups(Page page, Criteria criteria) throws Exception {
        D7PageUtils.daoPageToPage(groupService.findDefaultGroupByPage(D7PageUtils.buildQueryParams(criteria, page)), page);
    }

    @DataProvider
    public void loadGroupMembers(Page<GroupMemberVO> page, String groupId, String type) throws Exception {
        Map params = new HashMap();
        params.put("type", type);
        params.put("groupId", groupId);
        D7PageUtils.buildQueryParams(params, page);
        D7PageUtils.daoPageToPage(groupService.loadGroupMembers(params), page);
    }

    @DataResolver
    public void saveGroups(Collection<DefaultGroupVO> groups) throws Exception {
        groupService.saveMap(D7PageUtils.assembleDatasetMap(groups));
    }

    @Expose
    public void deleteMember(String memberId) throws Exception {
        groupService.delete(memberId);
    }

    @Expose
    public String insertGroupMembers(Collection<String> ids, String groupId, String type) throws Exception {
        return groupService.insertGroupMembers(ids, groupId, type);
    }
}
